﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
namespace Webcar.Controllers
{
    public partial class SystemController : BaseControler
    {
        //
        // GET: /System/

        public ActionResult Settings()
        {
            CurrentUser.EnsureAdmin();
            return View();
        }
        public ActionResult SettingsPartialView(string id)
        {
            var list = Webcar.Entitys.WebcarDataProvider.DB.BS_CODE.Where(a => a.BASE_KIND == id).ToList();
            return PartialView(list);
        }
        public ActionResult DeleteSettings(string id)
        {
            CurrentUser.EnsureAdmin();
            using (var DB = new WebcarContext())
            {
                var item = DB.BS_CODE.Find(id);
                if (item != null)
                {
                    DB.BS_CODE.Remove(item);
                }
                DB.SaveChanges();
                return RedirectToAction("Settings", "System", new { id = item.BASE_KIND });
            }
            
        }

        public ActionResult EditSetting(string id, string superid)
        {
            BS_CODE item = null;
            if (id.IsNotNull())
            {
                item = WebcarDataProvider.DB.BS_CODE.Find(id);
            }
            if (item == null)
            {
                item = new BS_CODE();
                item.BASE_KIND = superid;
            }
            return PartialView(model: item);
        }
        public ActionResult EditSettingSave(BS_CODE model)
        {
            try
            {
                CurrentUser.EnsureAdmin();
                if (model.BASE_NAME.IsNull())
                {
                    return Content("请输入参数名称。");
                }
                if (model.BASE_KIND.IsNull())
                {
                    return Content("请输入参数类型。");
                }

                BS_CODE item = null;
                using (var DB = new WebcarContext())
                {
                    if (model.BASE_CODE.IsNotNull())
                    {
                        item = DB.BS_CODE.Find(model.BASE_CODE);
                    }
                    else
                    {
                        item = new BS_CODE();

                        DB.BS_CODE.Add(item);
                    }
                    model.CopyPropertyTo(item);
                    //item.use_flag = "T";
                    if (item.BASE_CODE.IsNull())
                    {
                        item.BASE_CODE = "B" + DB.GetSearialId().ToString();
                    }
                    DB.SaveChanges();
                }
                return SuccessReload();
            }
            catch (Exception ex)
            {
                return Content("错误:" + ex.Message);
            }
        }
    }
}
